package parkshop.pmcourse.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.MallAdministrator;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.domain.ShopOwner;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.mapping.ShopMapper;
import parkshop.pmcourse.mapping.ShopOwnerMapper;
import parkshop.pmcourse.service.ShopOwnerService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class ShopOwnerServiceImpl implements ShopOwnerService{

	@Autowired
	private ShopOwnerMapper shopOwnerMapper;
	@Autowired
	private ShopMapper shopMapper;
	
	@Override
	public User signin(String name, String password) {

		ShopOwner shopOwner=shopOwnerMapper.selectByName(name);
		if(shopOwner!=null && shopOwner.getIsActived() == 1 && password.equals(shopOwner.getPassword())&&
				(shopOwner.getIssuspended()==null||ConstantUtil.wasBlackList!=shopOwner.getIssuspended())){
			
			Shop shop=shopMapper.selectByShopOwnerId(shopOwner.getId());
			User user=new User(shopOwner);
			if(shop==null)
				return null;
			if(ConstantUtil.shopWasApproved!=shop.getIsapproved())
				user.setAddress("shopWasNotApproved");
			return user;
		}
		return null;
	}

	@Override
	public boolean signup(User user,String shopName) {
		if(user==null)
			return false;
		ShopOwner temp=shopOwnerMapper.selectByName(user.getName());
		if(null!=temp)
			return false;
	
		ShopOwner shopOwner=new ShopOwner(user);
		shopOwner.setIsdelete((byte) (1-ConstantUtil.wasBlackList));
		shopOwner.setIssuspended((byte) (1-ConstantUtil.wasDelete));
		int rows=shopOwnerMapper.insertSelective(shopOwner);
		
		// apply new shop
		user.setId(shopOwnerMapper.selectByName(user.getName()).getId());
		boolean flag=openShop(user,shopName);
		
		return rows==1&&flag?true:false;
	}
	private boolean openShop(User user, String shopName) {
		// TODO Auto-generated method stub
		Shop shop=new Shop();
		shop.setContacts(user.getName());
		shop.setEmail(user.getMail());
		shop.setIsapproved(Byte.valueOf((byte) (1-ConstantUtil.shopWasApproved)));
		shop.setIsdelete(Byte.valueOf((byte) (1-ConstantUtil.wasDelete)));
		shop.setIssuspended(Byte.valueOf((byte) (1-ConstantUtil.wasBlackList)));
		shop.setName(shopName);
		shop.setPhone(user.getPhone());
		shop.setShopOwnerId(user.getId());
		if(shopMapper.selectByShopOwnerId(user.getId())!=null)
			return false;
		
		int rows=shopMapper.insertSelective(shop);
		return rows==1?true:false;
	}

	@Override
	public boolean updatePersonInfo(User user) {
		// TODO Auto-generated method stub
		ShopOwner shopOwner=new ShopOwner(user);
		int rows=shopOwnerMapper.updateByPrimaryKeySelective(shopOwner);
		return rows==1?true:false;
	}

	@Override
	public Shop getMyShop(Long id) {
		// TODO Auto-generated method stub
		return shopMapper.selectByShopOwnerId(id);		
	}

	@Override
	public boolean updateShopInfo(Shop shop) {
		// TODO Auto-generated method stub
		int rows=shopMapper.updateByPrimaryKeySelective(shop);
		return rows==1?true:false;
	}

	@Override
	public boolean activeUser(String code) {
		;
		ShopOwner shopOwner = shopOwnerMapper.getShopOwnerByActiveCode(code);
		if (shopOwner == null) {
			return false;
		}
		shopOwner.setIsActived((byte)1);
		int rows = shopOwnerMapper.updateShopOwnerByActiveCode(shopOwner);
		return rows==1?true:false;
	}

	

}
